package com.hmz.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hmz.backend.entity.ChatRoom;
import com.hmz.backend.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChatRoomMapper extends BaseMapper<ChatRoom> {
    //通过聊天室ID动态建立消息表
    @Insert("create table message${chatRoomId} ( " +
            "id int auto_increment primary key," +
            "sender_id int not null," +
            "receiver_id int not null," +
            "content varchar(1000) charset utf8mb3 not null," +
            "type int null," +
            "create_time datetime null" +
            ");")
    boolean createMessageTable(@Param("chatRoomId") int chatRoomId);
    //批量插入聊天室实体
    @Insert("INSERT INTO chatcove.chat_room values ${s}")
    void insertList(String s);
    //批量插入聊天室所属关系
    @Insert("INSERT INTO person values ${s}")
    void insertPersonList(String s);
    //添加成员和聊天室的关系
    @Insert("INSERT INTO person values (NULL,${userId},${chatRoomId})")
    boolean joinChatRoom(@Param("userId") int userId, @Param("chatRoomId") int chatRoomId);
    //获取用户所在的聊天室
    @Select("select chat_room.id,create_time time,chat_room_name,avatar,num " +
            "from chatcove.person left join  chatcove.chat_room  " +
            "on chat_room.id = person.chatroom_id " +
            "where user_id = ${ userId } and chat_room_name is not null ")
    List<ChatRoom> getMyChatRoomList ( int userId );
    //用户搜索聊天室
    @Select("select chat_room.id,create_time time,chat_room_name,avatar,num " +
            "from  chatcove.chat_room left join chatcove.person " +
            "on chat_room.id = person.chatroom_id " +
            "where chat_room_name like CONCAT('%', #{chatRoomName}, '%') and  person.user_id <> ${userId} ")
    List<ChatRoom> searchChatRoom (@Param("chatRoomName") String chatRoomName,@Param("userId") int userId);

    //获取聊天室下的用户列表
    @Select("select user.user_id,user_name,avatar, user_type " +
            "from  chatcove.user left join chatcove.person " +
            "on person.user_id = user.user_id " +
            "where chatroom_id = ${chatRoomId} ")
    List<User> getChatRoomUserList (int chatRoomId);
    //修改指定聊天室的成员数量
    @Update("update chat_room set num = num ${updateNum} where id = ${chatRoomId}")
    boolean updateChatRoomPersonNum( @Param("chatRoomId") int chatRoomId, @Param("updateNum") String updateNum);
    //从成员表中删除加入关系
    @Delete("delete from person where user_id = ${userId} and chatroom_id = ${chatRoomId} ")
    boolean deletePerson(@Param("userId") int userId,@Param("chatRoomId") int chatRoomId);
    //删除对应的消息表
    @Delete("drop table message${chatRoomId}")
    boolean deleteMessageTable(@Param("chatRoomId") int chatRoomId);
    //获取所有的好友用户
    @Select("select user_id userId , user_name, avatar, user_type " +
            "from user " +
            "where user_id in" +
            " (select" +
            "   case" +
            "       when friend.user_id = ${userId} then friend_id" +
            "       else friend.user_id" +
            "   end as user_id" +
            "  from friend " +
            "  where friend.user_id = ${userId} or friend.friend_id = ${userId})")
    List<User> getFriendUserList(@Param("userId") int userId);
    //查询好友聊天室列表
    @Select("select chat_room.id,create_time time,avatar,num " +
            "from chat_room " +
            "where id in (select chatroom_id from person where user_id = ${userId} ) " +
            "and chat_room.chat_room_name IS NULL ")
    List<ChatRoom> getFriendChatRoomList(@Param("userId") int userId);


}
